import instance from "@/http/index.js"

// -------------------------------- 用户信息 --------------------------------------------

// 获取用户信息
export const getUserInfoAPI = (id) => {
  return instance({
    url: "/api/getUserInfo",
    method: "post",
    data: {
      id
    }
  })
}

// 绑定账号
export const bindAccountAPI = (data) => {
  const { onlyId, url, account } = data
  return instance({
    url: "/user/bindAccount",
    method: "post",
    data: {
      onlyId,
      url,
      account
    }
  })
}

// 修改姓名
export const updateNameAPI = (data) => {
  const { id, name } = data
  return instance({
    url: "/user/updateName",
    method: "post",
    data: {
      id,
      name
    }
  })
}

// 修改性别
export const updateSexAPI = (data) => {
  const { id, sex } = data
  return instance({
    url: "/user/updateSex",
    method: "post",
    data: {
      id,
      sex
    }
  })
}

// 修改邮箱
export const updateEmailAPI = (data) => {
  const { id, email } = data
  return instance({
    url: "/user/updateEmail",
    method: "post",
    data: {
      id,
      email
    }
  })
}

// 修改密码
export const updatePasswordAPI = (data) => {
  const { id, oldPassword, newPassword } = data
  return instance({
    url: "/user/updatePassword",
    method: "post",
    data: {
      id,
      oldPassword,
      newPassword
    }
  })
}

// -------------------------------- 用户管理 --------------------------------------------

// 创建管理员  参数: account, password, name, email, sex , department, identity, create_time
export const createAdminAPI = (data) => {
  const { account, ...identity } = data
  return instance({
    url: "/user/creatAdmin",
    method: "post",
    data: {
      account,
      ...identity
    }
  })
}

// 获取管理员列表  参数: identity
export const getAdminListAPI = (identity) => {
  return instance({
    url: "/user/getAdminList",
    method: "post",
    data: {
      identity
    }
  })
}

// 编辑管理员信息 参数: id,  name, email, sex , department
export const updateAdminInfoAPI = (data) => {
  const { id, ...department } = data
  return instance({
    url: "/user/updateAdminInfo",
    method: "post",
    data: {
      id,
      ...department
    }
  })
}

// 管理员降职为用户 参数: id
export const updateAdminToUserAPI = (id) => {
  return instance({
    url: "/user/updateAdminToUser",
    method: "post",
    data: {
      id
    }
  })
}

// 用户升为管理员 参数: 
export const updateUserToAdminAPI = (data) => {
  const { identity, id } = data
  return instance({
    url: "/user/updateUserToAdmin",
    method: "post",
    data: {
      identity,
      id
    }
  })
}

//  通过账号搜索用户 参数: account indentity
export const searchUserByAccountAPI = (data) => {
  const { account, identity } = data
  return instance({
    url: "/user/searchUserByAccount",
    method: "post",
    data: {
      account,
      identity
    }
  })
}


// 冻结用户 把 status 置为 1  参数: status, id
export const freezeUserAPI = (status, id) => {
  return instance({
    url: "/user/freezeUser",
    method: "post",
    data: {
      status,
      id
    }
  })
}

// 解冻用户 把 status 置为 0  参数: id
export const unfreezeUserAPI = (status, id) => {
  return instance({
    url: "/user/unfreezeUser",
    method: "post",
    data: {
      status,
      id
    }
  })
}

// 获取冻结用户列表  参数: 无
export const getFreezeUserListAPI = () => {
  return instance({
    url: "/user/getFreezeUserList",
    method: "get"
  })
}

// 删除用户 参数: id account
export const deleteUserAPI = (data) => {
  const { id, account } = data
  return instance({
    url: "/user/deleteUser",
    method: "delete",
    data: {
      id,
      account
    }
  })
}

// 通过部门获取用户列表
export const getUserListByDepartmenAPI = (department) => {
  return instance({
    url: "/user/getUserListByDepartment",
    method: "post",
    data: {
      department
    }
  })
}


// 获取对应身份的用户总数 参数: identity
export const getUserCountByIdentityAPI  = (identity) => {
  return instance({
    url: "/user/getUserCountByIdentity",
    method: "post",
    data: {
      identity
    }
  })
}

//根据条目个数截取用户列表(分页)  参数: identity, pager
export const getUserListByPagerAPI = (data) => {
  const { identity, pager } = data
  return instance({
    url: "/user/getUserListByPager",
    method: "post",
    data: {
      identity,
      pager
    }
  })
}

// 获取部门列表
export const getDepartmentListAPI = () => {
  return instance({
    url: "/user/getDepartmentList",
    method:"get",
  })
}